/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.mes.controller;

import com.xy.biz.mes.domain.converter.ManufactureTaskConverter;
import com.xy.biz.mes.domain.converter.ManufactureTaskGoodsConverter;
import com.xy.biz.mes.domain.converter.ManufactureTaskRecordConverter;
import com.xy.biz.mes.domain.query.ManufactureTaskGoodsQuery;
import com.xy.biz.mes.domain.query.ManufactureTaskRecordQuery;
import com.xy.biz.mes.domain.req.*;
import com.xy.biz.mes.domain.resp.GetManufactureTaskCodeResponse;
import com.xy.biz.mes.domain.resp.GetManufactureTaskGoodsResponse;
import com.xy.biz.mes.listener.ManufactureTaskListener;
import com.xy.biz.mes.service.ManufactureTaskService;
import com.xy.biz.mes.client.ManufactureTaskClient;
import com.xy.biz.mes.domain.query.ManufactureTaskQuery;
import com.xy.biz.mes.domain.resp.GetManufactureTaskRecordResponse;
import com.xy.biz.mes.domain.resp.GetManufactureTaskResponse;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * @author zsp
 * @date 2022-1-6
 */
@Api(tags = "生产任务服务")
@Slf4j
@RestController
@RequestMapping("/api/mes/task")
public class ManufactureTaskController implements ManufactureTaskClient {

    @Autowired
    private ManufactureTaskService manufactureTaskService;


    @Override
    public void addListener(ManufactureTaskListener listener) {
        manufactureTaskService.addListener(listener);
    }

    @Override
    public void removeListener(ManufactureTaskListener listener) {
        manufactureTaskService.removeListener(listener);
    }


    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllManufactureTask(@RequestBody Collection<SaveManufactureTaskRequest> collection) {
        return manufactureTaskService.saveAllManufactureTask(ManufactureTaskConverter.toBO(collection));
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateManufactureTask(@RequestBody SaveManufactureTaskRequest request) {
        return manufactureTaskService.updateManufactureTask(ManufactureTaskConverter.toBO(request));
    }

//    @Validatable
//    @PostMapping(value = "/delete")
//    public Result<?> deleteManufactureTask(@RequestBody GetManufactureTaskRequest request) {
//        return manufactureTaskService.deleteManufactureTask(request.getManufactureTaskId());
//    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetManufactureTaskResponse> getManufactureTask(@RequestBody GetManufactureTaskRequest request) {
        return Result.success(ManufactureTaskConverter.toResponse(manufactureTaskService.getManufactureTask(ManufactureTaskConverter.toQuery(request))));
    }

//    @PostMapping(value = "/list_by_ids")
//    @Override
//    public Result<List<GetManufactureTaskResponse>> listManufactureTaskById(@RequestBody Collection<Long> collection) {
//        return Result.success(ManufactureTaskConverter.toResponse(manufactureTaskService.listManufactureTaskById(collection)));
//    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetManufactureTaskResponse>> listManufactureTask(@RequestBody ListManufactureTaskRequest request) {
        return Result.success(ManufactureTaskConverter.toResponse(manufactureTaskService.listManufactureTask(ManufactureTaskConverter.toQuery(request))));
    }

    @PostMapping(value = "/list_owner_task_code")
    public Result<List<GetManufactureTaskCodeResponse>> listOwnerManufactureTaskCode(@RequestBody ListManufactureTaskRequest request) {
        return Result.success(manufactureTaskService.listOwnerManufactureTaskCode(ManufactureTaskConverter.toQuery(request)));
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/hand_over")
    public Result<?> handOverManufactureTask(@RequestBody HandOverRequest request) {
        if (CollectionUtils.isEmpty(request.getHandOverGoodsList())) {
            return Result.ofBadRequest("交付的货品不能为空");
        }
        return manufactureTaskService.handOverManufactureTask(request);
    }

    @PostMapping(value = "/check")
    public Result<?> checkManufactureTask(@RequestBody SaveManufactureTaskRequest request) {
        return manufactureTaskService.checkManufactureTask(ManufactureTaskConverter.toBO(request));
    }

    @Override
    public Result<Integer> countManufactureTask(@RequestBody ListManufactureTaskRequest request) {
        return Result.success(manufactureTaskService.countManufactureTask(ManufactureTaskConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetManufactureTaskResponse>> pageManufactureTask(@RequestBody ListManufactureTaskRequest request) {
        ManufactureTaskQuery query = ManufactureTaskConverter.toQuery(request);
        List<GetManufactureTaskResponse> list = ManufactureTaskConverter.toResponse(manufactureTaskService.listManufactureTask(query));
        int count = manufactureTaskService.countManufactureTask(query);
        return Result.success(new Page<>(list, count));
    }

    @ApiOperation("任务详情")
    @PostMapping(value = "/page_goods")
    public Result<Page<GetManufactureTaskGoodsResponse>> pageManufactureTaskGoods(@RequestBody ListManufactureTaskGoodsRequest request) {
        ManufactureTaskGoodsQuery query = ManufactureTaskGoodsConverter.toQuery(request);
        List<GetManufactureTaskGoodsResponse> list = ManufactureTaskGoodsConverter.toResponse(manufactureTaskService.listManufactureTaskGoods(query));
        int count = manufactureTaskService.countManufactureTaskGoods(query);
        return Result.success(new Page<>(list, count));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all_record")
    public Result<?> saveAllManufactureTaskRecord(@RequestBody SaveManufactureTaskRequest request) {
        return manufactureTaskService.saveAllManufactureTaskRecord(ManufactureTaskConverter.toBO(request));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all_equipment")
    public Result<?> saveAllManufactureTaskEquipment(@RequestBody SaveManufactureTaskRequest saveManufactureTaskRequest) {
        return manufactureTaskService.saveAllManufactureTaskEquipment(ManufactureTaskConverter.toBO(saveManufactureTaskRequest));
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/save_all_goods")
    public Result<?> saveAllManufactureTaskGoods(@RequestBody SaveManufactureTaskRequest saveManufactureTaskRequest) {
        return manufactureTaskService.saveAllManufactureTaskGoods(ManufactureTaskConverter.toBO(saveManufactureTaskRequest));
    }

    @PostMapping(value = "/list_record")
    @Validatable
    public Result<List<GetManufactureTaskRecordResponse>> listManufactureTaskRecord(@RequestBody GetManufactureTaskRequest request) {
        return Result.success(ManufactureTaskRecordConverter.toResponse(
                manufactureTaskService.listManufactureTaskRecord(
                        ManufactureTaskRecordQuery.builder()
                                .manufactureTaskId(request.getManufactureTaskId())
                                .build())));
    }

}
